; See http://gimpforums.com/thread-batch-conversion-of-images

(define (script-fu_spc2013 input_filepath processed_dir remove_colour enlarge)                     
  (let*
    (
      (image (car (gimp-file-load RUN-NONINTERACTIVE input_filepath input_filepath)))  ; load the image file
      (drawable (car (gimp-image-get-active-layer image)))                             ; get the id of the layer
      
      (threshold 0)             ; settings for gimp-by-color-select
      (antialias FALSE)
      (feather FALSE)
      (feather-radius 0.0)
      (sample-merged FALSE)
      
      (path-list (strbreakup input_filepath DIR-SEPARATOR))  ; break the file path into a list
      (input_filename (car (reverse path-list)))             ; get the file name
      
      ; Make a path for the destination file in the destination directory
      (output_filepath (unbreakupstr (append (butlast path-list) (list processed_dir input_filename)) DIR-SEPARATOR) ) 
    )
    (gimp-message (string-append "processing " input_filepath))  ; keep the user informed
    
    (catch () (gimp-image-convert-rgb image)) ; convert the image to RGB mode, but don't error if it's already in RGB mode
    (gimp-layer-add-alpha drawable)           ; add an alpha channel 
    
    ; make a selection based on colour
    (gimp-by-color-select drawable remove_colour threshold CHANNEL-OP-REPLACE antialias feather feather-radius sample-merged)
        
    (if (= (car (gimp-selection-is-empty image)) FALSE) ; check if there is a selection
      (gimp-edit-cut drawable) ; if there is a selection then delete the contents of the selection
    )
    
    (if (= enlarge TRUE) ; If we want to scale the image by 400%
      (let*
        (
          (new-width (* (car (gimp-image-width image)) 4))
          (new-height (* (car (gimp-image-height image)) 4))
        )
        (gimp-image-scale-full image new-width new-height  INTERPOLATION-NONE)
      )
    )
    (gimp-file-save RUN-NONINTERACTIVE image drawable output_filepath output_filepath) ; save the new file
    
    (catch () (gimp-display-new image)) ; only useful when running in interactive mode
    (catch () (gimp-image-delete image)); remove the image when runing in batchmode
    (gimp-displays-flush) ; update the display (for running in interactive mode)
  )
)

;; Uncomment these lines if you want to try processing individual images interactively
;(script-fu-register "script-fu_spc2013"
;		    "spc2013..."
;		    ""
;		    "I did this"
;		    "GPL License"
;		    "25/01/2013"
;		    ""
;                    SF-FILENAME   _"Input Filename" "Mic.png"
;                    SF-STRING   _"Output Directory" "processed"
;		    SF-COLOR _"Colour to remove" '(0 255 0)
;                   SF-TOGGLE "Enlarge" FALSE
;)

;(script-fu-menu-register "script-fu_spc2013" "<Image>/contributed/")


; Do the batch stuff
(define (batch_spc2013 file_extension destination_directory remove_colour enlarge)
  (let* 
    (
      (varFileList (cadr (file-glob (string-append "*" file_extension) 1))) ; make a list of all the files that match the file extension
    )
    (gimp-message-set-handler ERROR-CONSOLE) ; force messages to the error-console so they actually appear in the terminal window
    (if (not (file-exists? destination_directory))
      (error (string-append "Error: directory " destination_directory " doesn't exist")) ; you haven't created a directory to put the files into
      (if (= (file-type destination_directory) FILE-TYPE-DIR) 
        ()
        (error (string-append destination_directory " is not a directory")) ; you've already got an oridinary file with the same name as the desired destination directory
      )
    )
    
    ; loop through all the files in the list
    (for-each
      (lambda (input_filepath)
;       Thing to do goes here
        (script-fu_spc2013 input_filepath destination_directory remove_colour enlarge)
      )
      varFileList
    )
  )
)
